﻿/// <reference path="../CloudDesk/cDesk-2.81.js" />
var MyMenu = window.MyMenu = {};
/*获取一级菜单*/
MyMenu.getAMenu = function () {
    var amenu = new Array();
    $.ajax({
        type: 'POST',
        url: '../sysWebMenu/queryAMenuList',
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        async: false,
        success: function (data) {
            if (data != null && data != undefined && data != "") {
            	amenu = eval(data);
            }
        },
        error: function (e) {
            if (window.console && window.console.error) {
                window.console.error("Cloud Desk >> \n  " + "Create Desk getAMenu Error" + e.statusText);
            }
        }
    });
    return amenu;
};
/*获取二级菜单*/
MyMenu.getBMenu = function (aMenuId) {
    var bmenu = new Array();
    $.ajax({
        type: 'POST',
        url: '../sysWebMenu/queryBMenuList',
        data:{arg:aMenuId},
        dataType: 'json',
        async: false,
        success: function (data) {
            if (data != null && data != undefined && data != "") {
            	bmenu = eval(data);
            }
        },
        error: function (e) {
            if (window.console && window.console.error) {
                window.console.error("Cloud Desk >> \n  " + "Create Desk getBMenu Error" + e.statusText);
            }
        }
    });
    return bmenu;
};
/*获取快捷菜单*/
MyMenu.getQuickMenu = function (number) {
    var quickmenu = new Array();
    $.ajax({
        type: 'POST',
        url: '../sysWebMenu/queryQuick',
        dataType: 'json',
        async: false,
        success: function (data) {
            if (data != null && data != undefined && data != "") {
            	quickmenu = eval(data);
            }
        },
        error: function (e) {
            if (window.console && window.console.error) {
                window.console.error("Cloud Desk >> \n  " + "Create Desk getQuickMenu Error" + e.statusText);
            }
        }
    });
    return quickmenu;
};

//一级菜单
var amenuList = MyMenu.getAMenu();

//二级菜;
var bmenuList = new Array();
for (var i = 0; i < amenuList.length; i++) {
    var bmenu = MyMenu.getBMenu(amenuList[i].id);
    bmenuList.push(bmenu);
}

//创建桌面
var namelist = new Array();
for (var i = 0; i < amenuList.length; i++) {
    namelist.push(amenuList[i].name);
}

var desk = cDesk.DeskBox.getInstance(namelist);

//添加APP
for (var m = 0; m < amenuList.length; m++) {
    (function (m) {
        var bmenu = bmenuList[m];
        for (var n = 0; n < bmenu.length; n++) {
            (function (n) {
                var icon = bmenu[n].deskIcon;
                
                if (icon == null || icon == 'null' || icon == "") {
                    icon = "default.png";
                }
                var appoption = {
                    appUrl: "../"+bmenu[n].linkUrl,
                    appType: 'inapp',
                    appIcon: "../static/frame/CloudDesk/MenuIcon/" + icon,
                    appName: bmenu[n].name,
                    appIndex: bmenu[n].sn,
                    appData: bmenu[n],
                    appClickHandle: openApp,
                    appUpdateHandler: null
                };
                var app = cDesk.AppButton.getInstance(appoption);
                desk.AddAppToDesk(m, app);
            } (n));
        }
    } (m));
}
//5分钟一次执行onload代码，用于更新数字
//SoUpdateAppNums();
//function SoUpdateAppNums() {
//    for (var i = 0; i < applist.length; i++) {
//        var app = applist[i];
//        if (app && app.Update) {
//            app.Update();
//        }
//    }
//    setInterval("SoUpdateAppNums()", 1000 * 60 * 5);
//}
//点击APP
function openApp(appmodel) {
    var curDesk = desk.GetShowDesk();
    var WindowNum = cDesk.DeskBox.getWinNums(0);
    if (WindowNum < 9) {
        var win = cDesk.Windows.getInstance(desk, appmodel, {
            CloseCallback: function () {
                cDesk.Taskbar.DelTask(win);
            }
        });
        cDesk.Taskbar.AddTask(win);
    }
    else {
        uceip.dialog.tips("<h3>最多允许同时打开 <b>9</b> 个窗口！</h3>", 2);
    }
}
//加载主题
$.ajax({
	url: '../sysWebMenu/queryTheme',
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    success: function (url) {
        if (url != undefined && url != null && url != ""){
            document.body.style.background = "url('" + url + "')";
        }
    },
    error: function (e) {
        if (window.console && window.console.error) {
            window.console.error("Cloud Desk >> \n  " + "GetTheme Error" + e.statusText);
        }
    }
});
/*主题保存*/
function SaveTheme(img) {
    if (img != undefined && img != null && img != "") {
        $.ajax({
        	url: '../sysWebMenu/saveTheme',
            type: 'POST',
            data: {arg:img.themeImage},
            dataType: 'json',
            cache: false,
            success: function () { },
            error: function (e) {
                if (window.console && window.console.error) {
                    window.console.error("Cloud Desk >> \n  " + "SaveTheme Error" + e.statusText);
                }
            }
        });
    }
}
/*主题设置*/
var settheme = null;
function SettingTheme() {
    if (!settheme) {
        settheme = cDesk.Themes.getInstance();
    }
    AddTheme("抽象1", "../static/frame/CloudDesk/background/min/抽象1.png", "../static/frame/CloudDesk/background/抽象1.png", function (data) { SaveTheme(data); });
    AddTheme("抽象2", "../static/frame/CloudDesk/background/min/抽象2.png", "../static/frame/CloudDesk/background/抽象2.png", function (data) { SaveTheme(data); });
    AddTheme("抽象3", "../static/frame/CloudDesk/background/min/抽象3.png", "../static/frame/CloudDesk/background/抽象3.png", function (data) { SaveTheme(data); });

    AddTheme("动物1", "../static/frame/CloudDesk/background/min/动物1.png", "../static/frame/CloudDesk/background/动物1.png", function (data) { SaveTheme(data); });
    AddTheme("动物2", "../static/frame/CloudDesk/background/min/动物2.png", "../static/frame/CloudDesk/background/动物2.png", function (data) { SaveTheme(data); });
    AddTheme("动物3", "../static/frame/CloudDesk/background/min/动物3.png", "../static/frame/CloudDesk/background/动物3.png", function (data) { SaveTheme(data); });

    AddTheme("风景1", "../static/frame/CloudDesk/background/min/风景1.png", "../static/frame/CloudDesk/background/风景1.png", function (data) { SaveTheme(data); });
    AddTheme("风景2", "../static/frame/CloudDesk/background/min/风景2.png", "../static/frame/CloudDesk/background/风景2.png", function (data) { SaveTheme(data); });
    AddTheme("风景3", "../static/frame/CloudDesk/background/min/风景3.png", "../static/frame/CloudDesk/background/风景3.png", function (data) { SaveTheme(data); });

    AddTheme("卡通玄幻1", "../static/frame/CloudDesk/background/min/卡通玄幻1.png", "../static/frame/CloudDesk/background/卡通玄幻1.png", function (data) { SaveTheme(data); });
    AddTheme("卡通玄幻2", "../static/frame/CloudDesk/background/min/卡通玄幻2.png", "../static/frame/CloudDesk/background/卡通玄幻2.png", function (data) { SaveTheme(data); });
    AddTheme("卡通玄幻3", "../static/frame/CloudDesk/background/min/卡通玄幻3.png", "../static/frame/CloudDesk/background/卡通玄幻3.png", function (data) { SaveTheme(data); });

    AddTheme("绿色1", "../static/frame/CloudDesk/background/min/绿色1.png", "../static/frame/CloudDesk/background/绿色1.png", function (data) { SaveTheme(data); });
    AddTheme("绿色2", "../static/frame/CloudDesk/background/min/绿色2.png", "../static/frame/CloudDesk/background/绿色2.png", function (data) { SaveTheme(data); });
    AddTheme("绿色3", "../static/frame/CloudDesk/background/min/绿色3.png", "../static/frame/CloudDesk/background/绿色3.png", function (data) { SaveTheme(data); });

    AddTheme("美女1", "../static/frame/CloudDesk/background/min/美女1.png", "../static/frame/CloudDesk/background/美女1.png", function (data) { SaveTheme(data); });
    AddTheme("美女2", "../static/frame/CloudDesk/background/min/美女2.png", "../static/frame/CloudDesk/background/美女2.png", function (data) { SaveTheme(data); });
    AddTheme("美女3", "../static/frame/CloudDesk/background/min/美女3.png", "../static/frame/CloudDesk/background/美女3.png", function (data) { SaveTheme(data); });
}
//主题面板添加主题
function AddTheme(name, icon, image, callback) {
    settheme.AddTheme({
        themeName: name,
        themeIcon: icon,
        themeImage: image,
        themeCss: '',
        setCallback: callback
    });
}
//创建工具栏
var toolbar = cDesk.Toolbar.getInstance(desk, {
    setBtn1Title: "个人信息",
    setBtn2Title: "主题设置",
    setBtn3Title: "个人设置",
    setBtn4Title: "显示桌面",
    setBtn1Handler: function (sender) {
    	modifyPwdDialog();
    },
    setBtn2Handler: function (sender) {
        SettingTheme();
    },
    setBtn3Handler: function (sender) {
    	modifyPwdDialog();
    },
    setBtn4Handler: function (sender) {
        /*显示/隐藏窗口*/
        var showState = sender.getAttribute("showState");
        var deskBox = desk.GetShowDesk();
        var nodeList = deskBox.childNodes;
        for (var i = 0; i < nodeList.length; i++) {
            var node = nodeList[i];
            if (node.getAttribute("class").toString() == "deskWindow") {
                node.style.display = showState;
            }
        }
        if (showState === "none") {
            sender.setAttribute("showState", "block");
        }
        else {
            sender.setAttribute("showState", "none");
        }
    },
    outBtnHandler: function () {
        if (confirm('您确定要退出系统吗？')) {
        	window.location = '../logout';
        }
    }
});

//快捷菜单
var quickmenuList = MyMenu.getQuickMenu();
for (var i = 0; i < quickmenuList.length; i++) {
    if (i >= 5) {
        break;
    }
    var menu = quickmenuList[i];
    var icon = menu.deskIcon;
    
    if (icon == null || icon == 'null' || icon == "") {
        icon = "default.png";
    }
    var app = cDesk.AppButton.getInstance({
    	 appUrl: "../"+bmenu[n].linkUrl,
        appType: 'inapp',
        appIcon: "../static/frame/CloudDesk/MenuIcon/" + icon,
        appName: menu.Name,
        appIndex: menu.sn,
        appData: menu,
        appClickHandle: openApp
    });
    toolbar.AddAppbutton(app);
}



